package com.ruoyi.user.controller;

import com.ruoyi.common.core.cg.dto.AddCommunityDto;
import com.ruoyi.common.core.cg.pojo.User;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService service;

    /**
     *  登录
     * @param loginUser
     * @return
     */
    @RequestMapping("login")
    public Result login(@RequestBody User loginUser){
        try {
            return service.login(loginUser);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"登录失败");
        }
    }

    /**
     * 查询用户所属的团的个数
     * @param userId
     * @return
     */
    @RequestMapping("getUserGroup")
    public Result getUserGroup(Integer userId){
        try {
            Integer groupCount = service.getUserGroup(userId);
            return new Result(true,"查询成功",groupCount);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    /**
     * 查询用户未提货或者已提货的订单个数
     * @param userId
     * @param state
     * @return
     */
    @RequestMapping("getUserOrderByState")
    public Result getUserOrderByState(Integer userId,Integer state){
        try {
            Integer count = service.getUserOrderByState(userId,state);
            return new Result(true,"查询成功",count);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    /**
     * 用户进入拼团点
     * @param dto
     * @return
     */
    @RequestMapping("joinCommunity")
    public Result joinCommunity(@RequestBody AddCommunityDto dto){
        try {
            service.joinCommunity(dto);
            return new Result(true,"进团成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"进团失败");
        }
    }

}
